(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:loginDialog
     * @description
     * # loginDialog
     */
    angular.module('webApp')
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.selectproductcat.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('selectProductcat', ["$rootScope", "helper", function($rootScope, helper) {
            return {
                restrict: 'EA',
                template: "<span><a ng-show='!stype' href='javascript:;' class='sui-btn btn-link' ng-click='showPro()'>+ {{productcatTitel ||'选择产品线'|translate}}</a><span ng-show='stype' >{{productcatTitel}}</span></span>",
                require:'ngModel',
                scope: {
                    ngModel:"=",
                    stype:"@",
                    rtype:"@",
                    callback:"&",
                },
                controller: ['$scope', 'authService', 'sessionService', 'dataService', '$compile', 'helper', '$templateCache','$timeout', function($scope, authService, sessionService, dataService, $compile, helper, $templateCache,$timeout) {
                    $scope.productcat={};
                    $scope.showPro=function(){
                        $scope.initData().then(showSelect)
                    }
                    $scope.initData=function(pageNo){
                        pageNo=pageNo?pageNo:1;
                        $scope.page_size=10;
                        var query={page_no:pageNo,page_size:$scope.page_size};
                          if ($scope.pid) {
                                query.query = { "pid": $scope.pid };
                            }
                       return dataService.productcatGet(query).then(function(r){
                            if(r.Msg){

                            }else{
                              $scope.productcats=r.shop;
                              $scope.productcatsTotal = r.total_count;
                              if($scope.productcatTitel){
                                var title_Array=$scope.productcatTitel.split(",");
                                if(_.isArray(title_Array)){
                                    _.map(title_Array,function(title){
                                        _.map($scope.productcats,function(productcat){
                                            if(productcat.name==title){
                                                productcat.is_checked=true;
                                            }
                                        })
                                    });
                                }else{
                                    _.map($scope.productcats,function(productcat){
                                        if(productcat.name==title_Array){
                                            productcat.is_checked=true;
                                        }else{
                                            productcat.is_checked=false;
                                        }
                                    })
                                }
                              }
                            }
                        });
                    }
                    $scope.addProductcat=function(productcat){
                        if($scope.rtype){
                             $scope.productcat=productcat;
                        }else{
                            if(productcat.is_checked){
                             $scope.productcat[productcat.id]=productcat;
                            }else{
                                delete $scope.productcat[productcat.id];
                            }
                        }
                    }
                    this.closeWin = function() {
                        loadingToggle(true);
                        if ($scope.loginWin) {
                            $scope.loginWin.modal("hide");
                        }
                    }
                     function showSelect() {
                        $scope.error = null;
                        //弹出
                      $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: '{{"need_login"|translate}}',
                            body: $templateCache.get("views/directives.selectproductcat.html"),
                            okBtn: '{{"login"|translate}}',
                            closeBtn: true,
                            transition: true,
                            width: 'large',
                            height:300,
                            show: function(e) {
                                console.log($scope.productcatTitel);
                                $compile($(e.currentTarget).contents())($scope);
                            },
                            okHide: function(e) {
                                $timeout(function(){
                                    if(_.size($scope.productcat)>0){
                                        console.log("333",$scope.productcatTitel,$scope.rtype,$scope.productcatTitel&&!$scope.rtype);
                                         if((!$scope.rtype)){
                                            console.log("2222");
                                            if(!$scope.productcatTitel){
                                                $scope.productcatTitel="";
                                            }
                                            _.map($scope.productcat,function(productcat){
                                                console.log("111",productcat);
                                                 if($scope.productcatTitel){
                                                    if(!$scope.productcatTitel.match(productcat.name)){
                                                          $scope.productcatTitel+=','+productcat.name;
                                                    }
                                                }else{
                                                 $scope.productcatTitel+=productcat.name;
                                                }
                                            });
                                        }else{
                                            $scope.productcatTitel=$scope.productcat.name;
                                        }
                                    }
                                        $scope.callback({productcat:$scope.productcat});

                                })
                            },
                            shown: function(e) {
                            }
                        });
                    }
                    $scope.initProductcat=function(id){
                        console.log(id,"222222222");
                        var splitV =",";
                        delete $scope.productcatTitel;
                        if(_.isNumber(id)){
                            console.log($scope.productcatTitel)
                             dataService.productcatOne(id).then(function(productcat){
                                    $scope.productcat=productcat;
                                    if($scope.productcatTitel&&!$scope.rtype){
                                          $scope.productcatTitel+=','+productcat.name;
                                    }else{
                                          $scope.productcatTitel=productcat.name;
                                    }
                            console.log($scope.productcatTitel,id);

                                }); 
                        }else {
                            var ids=id.split(',');
                            _.map(ids,function(mid){
                                 dataService.productcatOne(mid).then(function(productcat){
                                    $scope.productcat[productcat.id]=productcat;
                                    if($scope.productcatTitel){
                                          $scope.productcatTitel+=','+productcat.name;
                                    }else{
                                          $scope.productcatTitel=productcat.name;
                                    }
                                }); 
                            });
                            console.log($scope.productcatTitel);
                        }
                    }
                }],
                link: function(scope, elem, attrs, ctr) {
                     scope.$watch('ngModel', function(newValue, oldValue, scope) {
                        console.log("32",newValue,oldValue);
                        console.log(newValue);
                        if(!_.isUndefined(newValue)&&newValue!=0){
                            scope.initProductcat(newValue);
                        }else if(!newValue){
                           delete scope.productcatTitel;
                        }
                        // ctr.$setViewValue(newValue);
                        // scope.chosedItems = newValue;
                    });
                }
            }
        }]);
})();
